# -*- codeing = utf-8 -*-
# @Time: 2021/12/15 19:05
# @Author: Foxhuty
# @File: invigilation.py
# @Software: PyCharm
# @Based on python 3.9

import pandas as pd
import random
import numpy as np

filepath = r'D:\年级管理数据\高2021级\高一上\高一监考数据.xlsx'
df = pd.read_excel(filepath, index_col='id', sheet_name='teacher_names', dtype={'id': str})
exam_numbers = 9
room_numbers = 9

df_teachers = df[df['leader'] != 'yes']
df_teachers = df_teachers.loc[:, 'name']
df_care = df.loc[df['care'] == 'yes', 'name']

care_list = df_care.values.tolist()


# teachers_list = df_teachers.values
# print(teachers_list)
# random.shuffle(teachers_list)
# print(teachers_list)


def invigilation_talbe():
    teacher_list = df_teachers.values
    random.shuffle(teacher_list)

    # lst = [i for i in range(len(teacher_list))]
    # # random.seed(10)
    # random.shuffle(lst)
    # teacher_list_random = []
    # for i in lst:
    #     teacher_list_random.append(teacher_list[i])
    df_room = pd.DataFrame(columns=['Exam' + str(i + 1) for i in range(exam_numbers)],
                           index=['Room' + str(i + 1) for i in range(room_numbers)])
    df_room.index.rename('room_numbers', inplace=True)
    df_room.reset_index(inplace=True)
    nums = 0
    try:
        for i in df_room.columns[1:]:
            for k in df_room.index:
                df_room[i].at[k] = teacher_list[nums]
                if nums < len(teacher_list) - 1:
                    nums += 1
                else:
                    nums = 0
    except KeyError:
        df_room.fillna(np.NaN)
    else:
        print('successfully done')
    df_room.set_index('room_numbers', inplace=True)

    return df_room


df_room = invigilation_talbe()
print(df_room)
df_list_8 = df_room.loc[df_room.index[-2]].values.tolist()
df_list_9 = df_room.loc[df_room.index[-1]].values.tolist()
print(df_list_8)
df_list_8_9 = list(set(df_list_8).union(set(df_list_9)))

n = 0
ok_list = []
while len(ok_list) <= 1:
    df_room = invigilation_talbe()
    df_list_1 = df_room.loc['Room1'].values.tolist()
    df_list_2 = df_room.loc['Room2'].values.tolist()
    df_list_3 = df_room.loc['Room3'].values.tolist()
    df_list_4 = df_room.loc['Room4'].values.tolist()
    df_list_5 = df_room.loc['Room5'].values.tolist()
    df_list_6 = df_room.loc['Room6'].values.tolist()
    df_list_7 = df_room.loc['Room7'].values.tolist()
    df_list_8 = df_room.loc['Room8'].values.tolist()
    df_list_9 = df_room.loc['Room9'].values.tolist()
    df_list_8_9 = list(set(df_list_8).union(set(df_list_9)))

    for i in df_list_1, df_list_2, df_list_3, df_list_4, df_list_5, df_list_6, df_list_7:
        if not list(set(care_list).intersection(set(i))):
            ok_list.append(i)

print(ok_list)
# df_ok_8=ok_list[0]
# df_ok_9=ok_list[1]
